package com.bytecodezz.app.sketch.main.ui

import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.launcher.ARouter
import com.bytecodezz.app.base.extend.activity.isCurrentNavHostFragment
import com.bytecodezz.app.base.extend.ui.toast
import com.bytecodezz.app.sketch.common.router.AppNavigationConfig
import com.bytecodezz.app.sketch.common.router.CommonPath
import com.bytecodezz.app.sketch.common.base.BaseUIActivity
import com.bytecodezz.app.sketch.common.router.DrawPath
import com.bytecodezz.app.sketch.main.R
import com.bytecodezz.app.sketch.main.databinding.MainActivityLauncherUiBinding
import com.bytecodezz.app.sketch.main.model.LauncherUIViewModel
import com.bytecodezz.app.sketch.main.ui.home.MainFragment
import com.bytecodezz.app.sketch.main.ui.welcome.WelcomeFragment

class LauncherUI : BaseUIActivity<MainActivityLauncherUiBinding, LauncherUIViewModel>() {
    var isWelcomePager = true
    private var navigationGraph: Int? = null

    override fun initOtherObserver() {
        super.initOtherObserver()
        //初始化事件
        viewModel.showWelcomePager.observe(this) {
            if (it) {
                //显示欢迎页面
                isWelcomePager = true
                findNavController(R.id.launcher_main_nav_host_fragment).navigate(R.id.common_base_app_action_global_welcome_fragment)
            } else {
                //关闭欢迎页
                findNavController(R.id.launcher_main_nav_host_fragment).popBackStack()
                isWelcomePager = false
            }
        }
    }

    override fun onCreateViewBinding() = MainActivityLauncherUiBinding.inflate(layoutInflater)

    override fun onCreateViewModelClass() = LauncherUIViewModel::class.java

    override fun onActionPreSuper() {
        super.onActionPreSuper()
        //动态设置主题为正常主题，默认配置的是冷启动背景图片支持的主题
        setTheme(R.style.main_theme_LauncherUI_Normal)
    }

    override fun onActionStart(savedInstanceState: Bundle?) {
        setAppGraph()
        viewModel.initStart()
    }

    //设置默认启动页
    private fun setAppGraph() {
        navigationGraph = (ARouter.getInstance().build(
            CommonPath.APP_NAVIGATION_SERVICE
        ).navigation() as AppNavigationConfig).getAppNavigationGraph()
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.launcher_main_nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        navController.setGraph(
            navigationGraph!!
        )
    }

    //系统物理和虚拟返回键覆盖处理逻辑
    override fun onKeyBack(): Boolean {
        return when {
            isCurrentNavHostFragment(MainFragment::class.java) -> {
                //如果是首页，屏蔽返回键功能，并提供退出app的处理逻辑
                exitApp()
                true
            }
            isCurrentNavHostFragment(WelcomeFragment::class.java) -> {
                //如果是欢迎页，屏蔽返回键功能
                true
            }
            else -> {
                false
            }
        }
    }

    //退出app处理逻辑
    private var tempTime: Long = 0
    private fun exitApp() {
        val currentTime: Long = System.currentTimeMillis()
        if (currentTime - tempTime < 800) {
            super.onKeyBack()
        } else {
            showToast("再次点击退出软件")
            tempTime = currentTime
        }
    }

    fun testClick(view: android.view.View) {
        toast("测试Toast扩展功能")
    }

    fun intoDrawClick(view: android.view.View) {
        ARouter.getInstance().build(DrawPath.ACTIVITY_DRAW_MAIN_VIEW).navigation()
    }
}